import { equipment } from "@/api/index";
import { table } from "@/api";
import { mapState, mapActions } from "vuex";
export default {
  name: "EquipmentDetails",
  data () {
    return {
      total: 0,
      pageSizeOptions: [5, 10, 20, 50],
      pageNo: 1,
      pageSize: 10,
      queryParams: {
        devCode: undefined,
        inHome: undefined,
        status: undefined
      },
      tableData: [],
      inHomeList: [
        { id: 2, name: "全部" },
        { id: 1, name: "在公司" },
        { id: 0, name: "不在公司" }
      ], // 是否在公司
      statusList2: [
        { id: "", name: "全部" },
        { id: 0, name: "有效" },
        { id: 1, name: "已注销" }
      ], // 全部 => 状态
      statusList1: [
        { id: "", name: "全部" },
        { id: 0, name: "正常使用" },
        { id: 1, name: "闲置设备" }
      ], // 在公司 => 状态
      statusList0: [
        { id: "", name: "全部" },
        { id: 0, name: "待回收" },
        { id: 1, name: "待催缴" }
      ], // 不在公司 => 状态
      defaultStatusList: [],
      userInfo: sessionStorage.getItem('userInfo')
    };
  },
  computed: {
    // ...mapState({
    //   tenantId: (state) => state.login.tenantId,
    // }),
    isQueryParams () {
      return !this.queryParams.devCode && !this.queryParams.inHome && this.queryParams.inHome != 0 && !this.queryParams.status
    },
    isQuery () {
      return (this.queryParams.devCode && (!this.queryParams.inHome && this.queryParams.inHome != 0 && !this.queryParams.status))
    },
    user () {
      return JSON.parse(this.userInfo)
    },
    tenantId () {
      return this.user?.tenantId
    },
  },
  mounted () {
    this.locationInHone();
  },
  activated () {
    this.locationInHone();
    this.$nextTick(() => {
      this.$refs.table.doLayout();
    })
  },
  methods: {
    // 是否带参
    locationInHone () {
      if (this.$route.query.inHome) {
        this.queryParams.inHome = parseInt(this.$route.query.inHome);
        if (this.$route.query.status) {
          this.queryParams.status = parseInt(this.$route.query.status);
        }
        this.search();
      } else {
        this.queryParams.inHome = 2;
        this.search();
      }
    },
    // 切换页数大小
    handleSizeChange () {
      this.search();
    },
    // 分页
    handleCurrentChange () {
      this.search();
    },
    // 查询
    async search () {
      let params = {
        page: this.pageNo,
        size: this.pageSize,
        devCode: this.queryParams.devCode,
        inHome: this.queryParams.inHome,
        status: this.queryParams.status
      }
      if (this.isQueryParams || this.isQuery) {
        params.inHome = 2
      }
      let { code, data } = await equipment.equipmentList(params)
      if (code === 0) {
        this.tableData = data.records;
      }
      this.total = data.total;
    },
    clearInHome () {
      if (!this.queryParams.inHome) {
        this.queryParams.status = undefined;
      }
    },
    // 表格最后位置列
    lastPosition (row) {
      // 跳转电子回单部
      let path;
      if (process.env.NODE_ENV === "production") {
        path = "https://ntrajectory.wlyuan.com.cn/#/trajectoryLocation"
      } else {
        path = "http://192.168.1.168:81"
      }
      let deviceCode = row.iMEICode;
      let token = sessionStorage.getItem("token");
      window.open(path + "?deviceCode=" + deviceCode + "&token=" + token);
    },
    // 表格是否在公司
    handleSwitch (row) {
      const params = { deviceCode: row.iMEICode, inHome: row.inHome };
      this.$confirm(
        `${row.inHome == 1 ? `是否在公司？` : `是否不在公司？`}`,
        "提示"
      )
        .then(async (res) => {
          let response = await equipment.changeStatus(params);
          this.$nextTick(() => {
            this.$message.success(response.message);
            this.search();
            console.log(res);
          });
        })
        .catch((err) => {
          row.inHome = row.inHome == 1 ? 0 : 1;
          console.log(err);
        });
    },
    // 导入
    downLoad () {
      this.$router.push("/deviceImport");
    },
    // 导出
    async handleExport () {
      if (!this.tableData.length) {
        this.$message.warning("没有可要导出的数据！");
        return false;
      }
      let params = {
        args: { ...this.queryParams, tenantId: this.tenantId },
        name: "设备管理",
        type: 5
      };
      await table.submitFormContainer(`/export/task`, params);
      this.searchCount()
      this.$nextTick(() => {
        this.$message.success("导出功能正在后台执行中，请稍后去“导出文件”下载中查看！");
      });
    },
    // 查询导出未读数
    ...mapActions({
      searchCount: "login/exportCount"
    }),
    // 清除
    resetForm () {
      this.queryParams.devCode = undefined;
      this.queryParams.inHome = undefined;
      this.queryParams.status = undefined;
    }
  }
};